package main;

import handlers.PlayerAttackHandler;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import people.Character;
import storage.EnemyStorage;

public class NavigationFrame extends JFrame {

	private static final long serialVersionUID = 7467919523867592361L;
	
	JPanel border, attacks, navi;
	JButton[] attack; // 0 - melee, 1 - ranged, 2 - magic
	JButton[] movement; // 0 - up,  1 - left, 2 - right, 3 - down, 
	JButton[] blank;
	ImageIcon[] images;
	
	public NavigationFrame() {
		super("Adventure Land -- Navigation");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setupPanels();
		setupIcons();
		setupButtons();
		setupButtonListeners();
		this.add(border);
		this.setResizable(false);
		this.setBackground(Color.WHITE);
		this.setSize(300, 200);
		this.setVisible(true);
	}
	
	private void setupPanels() {
		border = new JPanel(new BorderLayout());
		attacks = new JPanel(new GridLayout(1, 3));
		navi = new JPanel(new GridLayout(3, 3));
		border.add(attacks, BorderLayout.NORTH);
		border.add(navi, BorderLayout.SOUTH);
		border.setBackground(Color.WHITE);
		attacks.setBackground(Color.WHITE);
		navi.setBackground(Color.WHITE);
	}
	
	private void setupIcons() {
		images = new ImageIcon[4];
		images[0] = createImageIcon("images/uparrow.png", "");
		images[1] = createImageIcon("images/leftarrow.png", "");
		images[2] = createImageIcon("images/rightarrow.png", "");
		images[3] = createImageIcon("images/downarrow.png", "");
	}
	
	private void setupButtons() {
		attack = new JButton[3];
		movement = new JButton[4];
		blank = new JButton[5];
		for (int i = 0; i < 5; i++) {
			blank[i] = new JButton(); 
			blank[i].setVisible(false);
		}
		blank[2].setText("Instructions");
		blank[2].setVisible(true);
		for (int i = 0; i < 3; i++) {
			attack[i] = new JButton();
		}
		attack[0].setText("Melee");
		attack[1].setText("Ranged");
		attack[2].setText("Magic");
		for (int i = 0; i < 3; i++) {
			attacks.add(attack[i]);
		}
		for (int i = 0; i < 4; i++) {
			movement[i] = new JButton();
			movement[i].setBackground(Color.WHITE);
			movement[i].setBorderPainted(false);
		}
		if (constants.GeneralConstants.SHOW_DIRECTIONAL_TEXT) {
			movement[0].setText("Up");
			movement[1].setText("Left");
			movement[2].setText("Right");
			movement[3].setText("Down");
		}
		movement[0].setIcon(images[0]);
		movement[1].setIcon(images[1]);
		movement[2].setIcon(images[2]);
		movement[3].setIcon(images[3]);
		navi.add(blank[0]);
		navi.add(movement[0]);
		navi.add(blank[1]);
		navi.add(movement[1]);
		navi.add(blank[2]);
		navi.add(movement[2]);
		navi.add(blank[3]);
		navi.add(movement[3]);
		navi.add(blank[4]);
	}
	
	private void setupButtonListeners() {
		blank[2].setFont(new Font("Arial", Font.BOLD, 10));
		blank[2].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Instructions.main(MainMap.args);
			}
		});
		movement[0].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainMap.moveChar(true, false, false, false);
			}
		});
		movement[1].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainMap.moveChar(false, false, true, false);
			}
		});
		movement[2].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainMap.moveChar(false, false, false, true);
			}
		});
		movement[3].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainMap.moveChar(false, true, false, false);
			}
		});
		attack[0].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y) != null) {
					PlayerAttackHandler.handle(EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y), 0);
				}
				else {
					System.out.println("There is no enemy there");
				}
			}
		});
		attack[1].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y) != null) {
					PlayerAttackHandler.handle(EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y), 1);
				}
				else {
					System.out.println("There is no enemy there");
				}
			}
		});
		attack[2].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y) != null) {
					PlayerAttackHandler.handle(EnemyStorage.findEnemyinXY(Character.c.x, Character.c.y), 2);
				}
				else {
					System.out.println("There is no enemy there");
				}
			}
		});
	
	}
	
	protected ImageIcon createImageIcon(String path, String description) {
		java.net.URL imgURL = getClass().getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}	
}